package tx.oopa.circuit;

import java.io.IOException;
import java.util.List;

import javax.swing.JFrame;

import tx.oopa.circuit.io.CircuitFileStrategy;
import tx.oopa.circuit.io.FileContext;
import tx.oopa.circuit.node.AbstractNode;
import tx.oopa.circuit.node.StartingNode;
import tx.oopa.circuit.view.NodeVisualizer;

public class Main {

    private static final String CIRCUIT_FILE_1 = "resources/circuit1.txt";

    /**
     * @param args
     */
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        FileContext context = new FileContext(new CircuitFileStrategy());
        try {
            if(context.fileIsValid(CIRCUIT_FILE_1)) {
                List<AbstractNode> nodes = (List<AbstractNode>) context
                        .readFromFile(CIRCUIT_FILE_1);
                for(AbstractNode node : nodes) {
                    if(node instanceof StartingNode) {
                        ((StartingNode) node).startCircuit();
                    }
                }

                // Initialize window
                JFrame window = new JFrame("Circuit");
                
                window.setResizable(true);
                window.setVisible(true);
                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                // Set content pane
                window.setContentPane(NodeVisualizer.visualizeNodes(nodes));
                
                window.setSize(NodeVisualizer.MAX_X, NodeVisualizer.MAX_Y);
            }
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }

}
